package cn.springcloud.fix.demo.proxy.custom;

import cn.springcloud.fix.demo.proxy.Person;

import java.lang.reflect.Method;

public class MeiPo implements GPInvocationHandler {

    private Person target;

    public Object getInstance(Person target) {
        this.target = target;
        System.out.println("被代理对象的class是:" + target.getClass());
        return GPPorxy.newProxyInstance(new GPClassLoader(), target.getClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("媒婆：我是媒婆，我替你找对象");
        System.out.println("------------");

        method.invoke(this.target, args);

        System.out.println("------------");
        System.out.println("媒婆：好的，我这就去找");

        return null;
    }

    /**
     * 自定义动态代理
     */
    public static void main(String[] args) {
        XiaoHong xiaoHong = new XiaoHong();
        Person proxy = (Person) new MeiPo().getInstance(xiaoHong);
        System.out.println("代理对象的class：" + proxy.getClass());
        proxy.findLove();
    }
}
